<?php

declare(strict_types=1);

namespace App\Util;

use App\Model\Guild\GuildHost;
use App\Model\Stat\StatOnlineLog;
use App\Model\User;
use App\Model\UserCoinOpLog;
use App\Model\UserDevice;
use App\Model\UserProfile;
use Illuminate\Support\Facades\Log;

/**
 * 临时工具箱
 */
class ToolUtil
{
    /**
     * 批量冻结用户
     */
    static function batchFreeze($userIds = [])
    {
        if (empty($userIds)) return false;
        if (!is_array($userIds)) $userIds = [$userIds];
        //修改推荐状态
        $count = count($userIds);
        User::whereIn('id', $userIds)->limit($count)->update(['status' => User::STATUS_FREEZE]);
        //融云踢下线
        ImUtil::kickOut($userIds);
        //设备封禁
        UserDevice::freezeUserDevice($userIds);
        Log::info(__METHOD__ . "==" . json_encode($userIds));
        return true;
    }

    /**
     * 封禁代理女用户
     */
    static function freezeGuildHosts($guildId = 0)
    {
        $guildHostIds = GuildHost::getHostIds($guildId);
        if (empty($guildHostIds)) return false;
        $count = count($guildHostIds);
        //取消女用户身份
        UserProfile::whereIn('user_id', $guildHostIds)->limit($count)->update(['role' => UserProfile::ROLE_USER]);
        //冻结账号
        $ret = self::batchFreeze($guildHostIds);
        Log::info(__METHOD__ . "==GuildId:{$guildId}==Ret:{$ret}");
        return $ret;
    }

    static function tmpAddCoinLog()
    {
        UserCoinOpLog::addRecord(292904, 5000, 2, '5月2-8通话周榜奖励第一');
        UserCoinOpLog::addRecord(118561, 2000, 2, '5月2-8通话周榜奖励第二');
        return 1;
    }

    static function checkInProvince($city, $province = '')
    {
        $data = [
            '山东' => ["济南", "青岛", "淄博", "威海", "曲阜", "临沂", "烟台", "枣庄", "聊城", "济宁", "菏泽", "泰安", "日照", "东营", "德州", "滨州", "莱芜", "潍坊"]
        ];
        if (!isset($data[$province])) return false;
        return in_array(trim($city, '市'), $data[$province]);
    }

    static function exportOnlineInfo($lmt = 1000, $file = 'onine_info.txt')
    {
        $data = StatOnlineLog::orderBy('id', 'desc')->limit($lmt)->get()->toArray();
        foreach ($data as $item) {
            $time = date('Y-m-d H:i:s', $item['stat_ts']);
            $str = "{$time}\t{$item['online_num']}\t{$item['online_user_num']}\t{$item['online_host_num']}\n";
            file_put_contents($file, $str, FILE_APPEND);
            dump($str);
        }
    }
}
